// 该文件专门用于创建整个应用的路由器
import { createRouter, createWebHistory } from 'vue-router'

//创建并暴露一个路由器
const Router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      name: "/login",
      path: "/login",
      component: () => import("../views/login/LoginPage"),
    },
    {
      name: "/",
      path: "/",
      redirect: "/login",
      component: () => import("../views/login/LoginPage"),
    },
    {
      name: "/system",
      path: "/system",
      redirect: "/home",
      component: () => import("../components/SystemHome"),
      children: [
        {
          name: "/home",
          path: "/home",
          component: () => import("../views/home/HomeIndex"),
        },
        {
          name: "/regionList", //区域列表
          path: "/regionList",
          component: () => import("@/views/regionManage/RegionList"),
        },
        {
          name: "/orderList", //订单列表
          path: "/orderList",
          component: () => import("@/views/orderManage/OrderList"),
        },
        {
          name: "/refundList", //退款列表
          path: "/refundList",
          component: () => import("@/views/orderManage/RefundList"),
        },
        {
          name: "/adoptOrderList", //认养订单列表
          path: "/adoptOrderList",
          component: () => import("@/views/orderManage/AdoptOrderList"),
        },
        {
          name: "/goodsList", //商品列表
          path: "/goodsList",
          component: () => import("@/views/goodsManage/GoodsList"),
        },
        {
          name: "/deliveryModeManage", //配送方式管理
          path: "/deliveryModeManage",
          component: () => import("@/views/goodsManage/deliveryModeManage"),
        },
        {
          name: "/cropsManageList", //农作物管理列表
          path: "/cropsManageList",
          component: () => import("../views/crops/CropManageList"),
        },

        {
          name: "/launchBatchManage", //投放批次管理
          path: "/launchBatchManage",
          component: () => import("../views/crops/LaunchBatchManage"),
        },
        {
          name: "/incomeExpenseManage", // 收支管理
          path: "/incomeExpenseManage",
          component: () => import("@/views/financeManage/IncomeExpenseManage"),
        },
        {
          name: "/withdrawalRecord", // 提现记录
          path: "/withdrawalRecord",
          component: () => import("@/views/financeManage/WithdrawalRecord"),
        },
        {
          name: "/invoiceManage", // 发票管理
          path: "/invoiceManage",
          component: () => import("@/views/financeManage/InvoiceManage"),
        },
      ],
    },
  ],
});
Router.beforeEach(function(to, from, next){
    console.log(from.path);
    console.log(to.path);
    if (to.path == '/system') {
        const token = localStorage.getItem('token')
        console.log(token);
        // next()
        if (token) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})
export default Router